// pages/busessReg/businessReg.js
import {
  MerchantRegister,
  getCity,
  uploadMerchant,
  bankList,
  bankBranchInfo
} from '../../api.js'
let app = getApp();
Page({
  data: {
    chooseProvince: false,
    showCity: false,
    showArea: false,
    province: [],
    city: [],
    area: [],
    index: 0,
    index2: 0,
    index3: 0,
    Images: {
      IDCARD_FRONT: '', //法人身份证
      IDCARD_BACK: '', //法人身份证反面
      SETTLE_BANKCARD: '', //银行卡
      OP_BANK_CODE: '', //银行开户许可证
      logo_url: '', //店铺LOGO
      HAND_IDCARD: '', // 手持身份证
      UNI_CREDIT_CODE: '', //统一社会信用
      CORP_CODE: '', //营业执照
      TAX_CODE: '', //税务登记证
      ORG_CODE: '', //组织机构代码证
    },
    toAuth: {
      merFullName: '', //商户全称
      merShortName: '', //商户品牌名/简称
      merCertType: 'UNI_CREDIT_CODE', //证件类型
      merCertNo: '', //证件号      
      merProvince: '', //商户经营所在省
      merCity: '', //所在市(编码)
      merDistrict: '', //区
      merAddress: '', //详细地址
      legalIdCard: '', //法人身份证号
      legalName: '', //法人姓名
      merContactName: '', //商户联系人姓名
      merContactPhone: '', //手机
      // merContactEmail: '', //邮箱
      taxRegistCert: '', //税务登记证编号
      accountLicense: '', //开户许可证编号
      orgCode: '', //组织机构代码
      isOrgCodeLong: 'false', //组织机构是否长期有效
      orgCodeExpiry: '2016-09-01', //组织机构代理证有效期
      bankCity: '', //开户市
      bankCode: '', //支行编码
      bankProvince: '', //开户省
      cardNo: '', //银行账户
      headBankCode: '', //开户总行编码
      openMini: 0, //是否开通小程序
      fileInfo: { // 照片信息
        IDCARD_FRONT: { }, //法人身份证
        IDCARD_BACK: {}, //法人身份证反面
        SETTLE_BANKCARD: {}, //结算银行卡
        OP_BANK_CODE: {}, //银行开户许可证
        logo_url: {}, // 手持身份证
        HAND_BANKCARD: {}, //手持银行卡
        UNI_CREDIT_CODE: {}, //统一社会信用
        CORP_CODE: {}, //营业执照
        TAX_CODE: {}, //税务登记证
        ORG_CODE: {}, //组织机构代码证
      }
    },
    merFullName_focus:false,//执照名称光标位置
    merShortName_focus:false,//门店名称光标位置
    merAddress_focus:false,//详细地址光标位置
    merCertNo_focus:false,//证件光标位置
    taxRegistCert_focus:false,//税务登记编号光标位置
    accountLicense_focus: false,//开户许可证编号光标位置
    orgCode_focus: false,//组织机构代码证光标位置
    legalName_focus: false,//法人姓名光标
    legalIdCard_focus: false,//法人身份证号光标
    merContactName_focus: false,//联系人姓名光标
    merContactPhone_focus: false,//联系人手机号光标
    // merContactEmail_focus: false,//联系人邮箱光标
  },
  onLoad: function(options) {
    console.log('企业认证', getCurrentPages())
    console.log('加载')
    getCity({
      token: app.globalData.token
    }).then(res => {
      console.log('省市区', res)
      if(res.data.code==666){
        this.setData({
          province: res.data.data
        })
      } else if (res.data.code == 111111) {
        wx.showToast({
          title: '登录失效，请重新登录',
          icon: 'none',
        })
        setTimeout(function () {
          wx.reLaunch({
            url: '/pages/user/login/login',
          })
        }, 1500)
      }

    })
  },
  onShow: function() {
    console.log('显示')
  },
  chooseProvince(e) {
    let label = e.currentTarget.dataset.address;
    this.setData({
      [label]: true
    })
    console.log('e', e)
  },
  //商户经营省
  bindProvince: function(e) {
    this.setData({
      index: e.detail.value,
    })
    if (this.data.city.length > 0) {
      this.setData({
        city: [],
        area: [],
        showCity: false,
        showArea: false
      })
    }
    let city = this.data.province[this.data.index].children;
    let merProvince = this.data.province[this.data.index].value;
    this.setData({
      city,
      'toAuth.merProvince': merProvince
    })
  },
  //商户经营市
  bindCity: function(e) {
    this.setData({
      index2: e.detail.value,
    })
    if (this.data.area.length > 0) {
      this.setData({
        area: [],
        showArea: false
      })
    }
    let area = this.data.city[this.data.index2].children;
    let merCity = this.data.city[this.data.index2].value;
    this.setData({
      area,
      'toAuth.merCity': merCity
    })
  },
  //商户经营区
  bindArea: function(e) {
    this.setData({
      index3: e.detail.value,
    })
    let merDistrict = this.data.area[this.data.index3].value;
    this.setData({
      'toAuth.merDistrict': merDistrict
    })
  },

  bindDate(e) {
    console.log(e)
    this.setData({
      'toAuth.isOrgCodeLong': e.detail.value
    })
  },
  bindDateChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      'toAuth.orgCodeExpiry': e.detail.value
    })
  },
  bindName(e) {
    var key = `${e.currentTarget.dataset.name}`;
    this.setData({
      [key]: e.detail.value
    })
  },
  //证件类型
  checkboxMerCertType(e) {
    console.log(e)
    this.setData({
      'toAuth.merCertType': e.detail.value
    })
  },

  //图片上传

  //身份证正面
  IDCARD_FRONT: function(obj) {
    var that = this
    wx.chooseImage({
      count: 1,
      success: function(res) {
        console.log(res)
        wx.showLoading({
          title: '正在加载图片',
          icon: 'loading'
        })
        var url = res.tempFilePaths;
        wx.uploadFile({
          url: 'https://tool.growthupup.com/api/public/index/upload/uploadMerchant',
          filePath: url[0],
          name: 'imgfile',
          formData: {
            'token': app.globalData.token
          },
          success: function(res) {
            var jsonStr = res.data;
            jsonStr = jsonStr.replace(" ", "");
            if (typeof jsonStr != 'object') {
              jsonStr = jsonStr.replace(/\ufeff/g, "");
              var jj = JSON.parse(jsonStr);
              res.data = jj;
            }
            if (res.data.code == 666) {
              wx.hideLoading();
              that.data.toAuth.fileInfo.IDCARD_FRONT.url = res.data.data.url
              that.data.toAuth.fileInfo.IDCARD_FRONT.thumb = res.data.data.thumb
              that.data.toAuth.fileInfo.IDCARD_FRONT._file = res.data.data.base_image
              that.data.Images.IDCARD_FRONT = res.data.data.url
              that.setData({
                Images: that.data.Images
              })
            } else if (res.data.code == 111111) {
              wx.showToast({
                title: '登录失效，请重新登录',
                icon: 'none',
              })
              setTimeout(function () {
                wx.reLaunch({
                  url: '/pages/user/login/login',
                })
              }, 1500)
            }
          }
        })
      },
    })
  },
  //身份证反面
  IDCARD_BACK: function(obj) {
    var that = this
    wx.chooseImage({
      count: 1,
      success: function(res) {
        console.log(res)
        wx.showLoading({
          title: '正在加载图片',
          icon: 'loading'
        })
        var url = res.tempFilePaths;
        wx.uploadFile({
          url: 'https://tool.growthupup.com/api/public/index/upload/uploadMerchant',
          filePath: url[0],
          name: 'imgfile',
          formData: {
            'token': app.globalData.token
          },
          success: function(res) {
            var jsonStr = res.data;
            jsonStr = jsonStr.replace(" ", "");
            if (typeof jsonStr != 'object') {
              jsonStr = jsonStr.replace(/\ufeff/g, "");
              var jj = JSON.parse(jsonStr);
              res.data = jj;
            }
            if (res.data.code == 666) {
              wx.hideLoading();
              that.data.toAuth.fileInfo.IDCARD_BACK.url = res.data.data.url
              that.data.toAuth.fileInfo.IDCARD_BACK.thumb = res.data.data.thumb
              that.data.toAuth.fileInfo.IDCARD_BACK._file = res.data.data.base_image
              that.data.Images.IDCARD_BACK = res.data.data.url
              that.setData({
                Images: that.data.Images
              })
            }
          }
        })
      },
    })
  },
  //手持身份证
  HAND_IDCARD: function(obj) {
    var that = this
    wx.chooseImage({
      count: 1,
      success: function(res) {
        console.log(res)
        wx.showLoading({
          title: '正在加载图片',
          icon: 'loading'
        })
        var url = res.tempFilePaths;
        wx.uploadFile({
          url: 'https://tool.growthupup.com/api/public/index/upload/uploadMerchant',
          filePath: url[0],
          name: 'imgfile',
          formData: {
            'token': app.globalData.token
          },
          success: function(res) {
            var jsonStr = res.data;
            jsonStr = jsonStr.replace(" ", "");
            if (typeof jsonStr != 'object') {
              jsonStr = jsonStr.replace(/\ufeff/g, "");
              var jj = JSON.parse(jsonStr);
              res.data = jj;
            }
            if (res.data.code == 666) {
              wx.hideLoading();
              that.data.toAuth.fileInfo.HAND_IDCARD.url = res.data.data.url
              that.data.toAuth.fileInfo.HAND_IDCARD.thumb = res.data.data.thumb
              that.data.toAuth.fileInfo.HAND_IDCARD._file = res.data.data.base_image
              that.data.Images.HAND_IDCARD = res.data.data.url
              that.setData({
                Images: that.data.Images
              })
            }
          }
        })
      },
    })
  },
  //营业执照或市场合同
  CORP_CODE: function(obj) {
    var that = this
    wx.chooseImage({
      count: 1,
      success: function(res) {
        console.log(res)
        wx.showLoading({
          title: '正在加载图片',
          icon: 'loading'
        })
        var url = res.tempFilePaths;
        wx.uploadFile({
          url: 'https://tool.growthupup.com/api/public/index/upload/uploadMerchant',
          filePath: url[0],
          name: 'imgfile',
          formData: {
            'token': app.globalData.token
          },
          success: function(res) {
            var jsonStr = res.data;
            jsonStr = jsonStr.replace(" ", "");
            if (typeof jsonStr != 'object') {
              jsonStr = jsonStr.replace(/\ufeff/g, "");
              var jj = JSON.parse(jsonStr);
              res.data = jj;
            }
            if (res.data.code == 666) {
              wx.hideLoading();
              that.data.toAuth.fileInfo.CORP_CODE.url = res.data.data.url
              that.data.toAuth.fileInfo.CORP_CODE.thumb = res.data.data.thumb
              that.data.toAuth.fileInfo.CORP_CODE._file = res.data.data.base_image
              that.data.Images.CORP_CODE = res.data.data.url
              that.setData({
                Images: that.data.Images
              })
            }
          }
        })
      },
    })
  },
  //税务登记证
  TAX_CODE: function(obj) {
    var that = this
    wx.chooseImage({
      count: 1,
      success: function(res) {
        console.log(res)
        wx.showLoading({
          title: '正在加载图片',
          icon: 'loading'
        })
        var url = res.tempFilePaths;
        wx.uploadFile({
          url: 'https://tool.growthupup.com/api/public/index/upload/uploadMerchant',
          filePath: url[0],
          name: 'imgfile',
          formData: {
            'token': app.globalData.token
          },
          success: function(res) {
            var jsonStr = res.data;
            jsonStr = jsonStr.replace(" ", "");
            if (typeof jsonStr != 'object') {
              jsonStr = jsonStr.replace(/\ufeff/g, "");
              var jj = JSON.parse(jsonStr);
              res.data = jj;
            }
            if (res.data.code == 666) {
              wx.hideLoading();
              that.data.toAuth.fileInfo.TAX_CODE.url = res.data.data.url
              that.data.toAuth.fileInfo.TAX_CODE.thumb = res.data.data.thumb
              that.data.toAuth.fileInfo.TAX_CODE._file = res.data.data.base_image
              that.data.Images.TAX_CODE = res.data.data.url
              that.setData({
                Images: that.data.Images
              })
            }
          }
        })
      },
    })
  },
  //组织机构代码证
  ORG_CODE: function(obj) {
    var that = this
    wx.chooseImage({
      count: 1,
      success: function(res) {
        console.log(res)
        wx.showLoading({
          title: '正在加载图片',
          icon: 'loading'
        })
        var url = res.tempFilePaths;
        wx.uploadFile({
          url: 'https://tool.growthupup.com/api/public/index/upload/uploadMerchant',
          filePath: url[0],
          name: 'imgfile',
          formData: {
            'token': app.globalData.token
          },
          success: function(res) {
            var jsonStr = res.data;
            jsonStr = jsonStr.replace(" ", "");
            if (typeof jsonStr != 'object') {
              jsonStr = jsonStr.replace(/\ufeff/g, "");
              var jj = JSON.parse(jsonStr);
              res.data = jj;
            }
            if (res.data.code == 666) {
              wx.hideLoading();
              that.data.toAuth.fileInfo.ORG_CODE.url = res.data.data.url
              that.data.toAuth.fileInfo.ORG_CODE.thumb = res.data.data.thumb
              that.data.toAuth.fileInfo.ORG_CODE._file = res.data.data.base_image
              that.data.Images.ORG_CODE = res.data.data.url
              that.setData({
                Images: that.data.Images
              })
            }
          }
        })
      },
    })
  },
  //统一社会信用代码证
  UNI_CREDIT_CODE: function(obj) {
    var that = this
    wx.chooseImage({
      count: 1,
      success: function(res) {
        console.log(res)
        wx.showLoading({
          title: '正在加载图片',
          icon: 'loading'
        })
        var url = res.tempFilePaths;
        wx.uploadFile({
          url: 'https://tool.growthupup.com/api/public/index/upload/uploadMerchant',
          filePath: url[0],
          name: 'imgfile',
          formData: {
            'token': app.globalData.token
          },
          success: function(res) {
            var jsonStr = res.data;
            jsonStr = jsonStr.replace(" ", "");
            if (typeof jsonStr != 'object') {
              jsonStr = jsonStr.replace(/\ufeff/g, "");
              var jj = JSON.parse(jsonStr);
              res.data = jj;
            }
            if (res.data.code == 666) {
              wx.hideLoading();
              that.data.toAuth.fileInfo.UNI_CREDIT_CODE.url = res.data.data.url
              that.data.toAuth.fileInfo.UNI_CREDIT_CODE.thumb = res.data.data.thumb
              that.data.toAuth.fileInfo.UNI_CREDIT_CODE._file = res.data.data.base_image
              that.data.Images.UNI_CREDIT_CODE = res.data.data.url
              that.setData({
                Images: that.data.Images
              })
            }
          }
        })
      },
    })
  },
  //银行开户许可证
  OP_BANK_CODE: function(obj) {
    var that = this
    wx.chooseImage({
      count: 1,
      success: function(res) {
        console.log(res)
        wx.showLoading({
          title: '正在加载图片',
          icon: 'loading'
        })
        var url = res.tempFilePaths;
        wx.uploadFile({
          url: 'https://tool.growthupup.com/api/public/index/upload/uploadMerchant',
          filePath: url[0],
          name: 'imgfile',
          formData: {
            'token': app.globalData.token
          },
          success: function(res) {
            var jsonStr = res.data;
            jsonStr = jsonStr.replace(" ", "");
            if (typeof jsonStr != 'object') {
              jsonStr = jsonStr.replace(/\ufeff/g, "");
              var jj = JSON.parse(jsonStr);
              res.data = jj;
            }
            if (res.data.code == 666) {
              wx.hideLoading();
              that.data.toAuth.fileInfo.OP_BANK_CODE.url = res.data.data.url
              that.data.toAuth.fileInfo.OP_BANK_CODE.thumb = res.data.data.thumb
              that.data.toAuth.fileInfo.OP_BANK_CODE._file = res.data.data.base_image
              that.data.Images.OP_BANK_CODE = res.data.data.url
              that.setData({
                Images: that.data.Images
              })
            }
          }
        })
      },
    })
  },
  //下一步
  next() {
    var _form = this.data.toAuth;
    var that = this
    var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
    var iso = reg.test(_form.legalIdCard);
    var isPhone = /^1[3|4|5|8][0-9]\d{4,8}$/;
    var isok = isPhone.test(_form.merContactPhone);
    console.log(_form)
    if (!_form.merFullName) {
      wx.showToast({
        title: '请填写执照名称',
        icon: 'none'
      })
      that.setData({
        merFullName_focus:true
      })
      return false;
    } else if (!_form.merShortName) {
      wx.showToast({
        title: '请填写门店名称',
        icon: 'none'
      })
      that.setData({
        merShortName_focus:true
      })
      return false;
    } else if (!_form.merCity || !_form.merDistrict || !_form.merProvince) {
      wx.showToast({
        title: '请选择经营地址',
        icon: 'none'
      })
      return false;
    } else if (!_form.merAddress) {
      wx.showToast({
        title: '请填写详细地址',
        icon: 'none'
      })
      that.setData({
        merAddress_focus: true
      })
      return false;
    } else if (_form.merCertType == 'UNI_CREDIT_CODE' && !_form.fileInfo.UNI_CREDIT_CODE.url) {
      wx.showToast({
        title: '请上传统一社会信用代码证',
        icon: 'none'
      })
      return false;
    } else if (_form.merCertType == 'CORP_CODE' && !_form.taxRegistCert) {
      wx.showToast({
        title: '请填写税务登记证编号',
        icon: 'none'
      })
      return false;
    } else if (_form.merCertType == 'CORP_CODE' && !_form.orgCode) {
      wx.showToast({
        title: '请填写组织机构代码',
        icon: 'none'
      })
      return false;
    } else if (_form.merCertType == 'CORP_CODE' && !_form.fileInfo.CORP_CODE.url) {
      wx.showToast({
        title: '请填上传营业执照或市场合同',
        icon: 'none'
      })
      return false;
    } else if (_form.merCertType == 'CORP_CODE' && !_form.fileInfo.TAX_CODE.url) {
      wx.showToast({
        title: '请填上传税务登记证',
        icon: 'none'
      })
      return false;
    } else if (_form.merCertType == 'CORP_CODE' && !_form.fileInfo.ORG_CODE.url) {
      wx.showToast({
        title: '请填上传组织机构代码',
        icon: 'none'
      })
      return false;
    } else if (!_form.merCertNo) {
      wx.showToast({
        title: '请填写证件号',
        icon: 'none'
      })
      that.setData({
        merCertNo_focus:true
      })
      return false;
    } else if (!_form.accountLicense) {
      wx.showToast({
        title: '请填写开户许可证编号',
        icon: 'none'
      })
      that.setData({
        accountLicense_focus: true
      })
      return false;
    } else if (!_form.legalName) {
      wx.showToast({
        title: '请填写法人姓名',
        icon: 'none'
      })
      that.setData({
        legalName_focus: true
      })
      return false;
    } else if (!_form.legalIdCard) {
      wx.showToast({
        title: '请填写法人身份证号',
        icon: 'none'
      })
      that.setData({
        legalIdCard_focus: true
      })
      return false;
    } else if (_form.legalIdCard && !iso) {
      wx.showToast({
        title: '请填写正确法人身份证号',
        icon: 'none'
      })
      that.setData({
        legalIdCard_focus: true
      })
      return false;
    } else if (!_form.merContactName) {
      wx.showToast({
        title: '请填写联系人姓名',
        icon: 'none'
      })
      that.setData({
        merContactName_focus: true
      })
      return false;
    } else if (!_form.merContactPhone) {
      wx.showToast({
        title: '请填写联系人手机号',
        icon: 'none'
      })
      that.setData({
        merContactPhone_focus: true
      })
      return false;
    } else if (_form.merContactPhone && !isok) {
      wx.showToast({
        title: '请填写正确联系人手机号',
        icon: 'none'
      })
      that.setData({
        merContactPhone_focus: true
      })
      return false;
    } else if (!_form.fileInfo.IDCARD_FRONT.url) {
      wx.showToast({
        title: '请填上传身份证正面',
        icon: 'none'
      })
      return false;
    } else if (!_form.fileInfo.IDCARD_BACK.url) {
      wx.showToast({
        title: '请填上传身份证反面',
        icon: 'none'
      })
      return false;
    } else if (!_form.fileInfo.OP_BANK_CODE.url) {
      wx.showToast({
        title: '请填上传银行开户许可证',
        icon: 'none'
      })
      return false;
    }
    wx.navigateTo({
      url: 'bankReg?toAuth=' + JSON.stringify(this.data.toAuth)
    })
  },
})